| Conditions | 2 |
| Paths | 4 |
| Total Lines | 64 |
| Lines | 64 |
| Ratio | 100 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* TODO |
||
| 172 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) {
|
||
| 173 | if (fullLog) {
|
||
| 174 | console.log(`${timeStamp()} Successfully logged in.`.green);
|
||
|
1 ignored issue
–
show
|
|||
| 175 | } |
||
| 176 | |||
| 177 | var ret = {};
|
||
| 178 | var $ = cheerio.load(ires.text); |
||
| 179 | |||
| 180 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3);
|
||
| 181 | ret.id = req.query.id; |
||
| 182 | ret.sem = req.query.sem || getSem(); |
||
| 183 | |||
| 184 | superagent |
||
| 185 | .post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list')
|
||
| 186 | .set(headers) |
||
| 187 | .type('form')
|
||
| 188 | .send({
|
||
| 189 | xqlbmc: '', |
||
| 190 | xnxqid: ret.sem, |
||
| 191 | xqlb: '' |
||
| 192 | }) |
||
| 193 | .end(function (err, iires) {
|
||
| 194 | if (err) {
|
||
| 195 | console.log(`${timeStamp()} Failed to reach exams page\n${err.stack}`.red);
|
||
|
1 ignored issue
–
show
|
|||
| 196 | res.send({ error: '获取成绩失败' });
|
||
| 197 | return next(err); |
||
| 198 | } |
||
| 199 | if (fullLog) {
|
||
| 200 | console.log(`${timeStamp()} Successfully entered exams page.`.green);
|
||
| 201 | } |
||
| 202 | |||
| 203 | $ = cheerio.load(iires.text); |
||
| 204 | |||
| 205 | ret.exams = {};
|
||
| 206 | ret['exams-count'] = 0; |
||
| 207 | |||
| 208 | $('#dataList tr').each(function (index) {
|
||
| 209 | if (index === 0) {
|
||
| 210 | return; |
||
| 211 | } |
||
| 212 | let element = $(this).find('td');
|
||
| 213 | let title = escaper.unescape(element.eq(3).text()); |
||
| 214 | |||
| 215 | let item = {
|
||
| 216 | time: escaper.unescape(element.eq(4).text()), |
||
| 217 | location: escaper.unescape(element.eq(5).text()), |
||
| 218 | seat: escaper.unescape(element.eq(6).text()) |
||
| 219 | }; |
||
| 220 | |||
| 221 | ret.exams[title] = item; |
||
| 222 | ret['exams-count']++; |
||
| 223 | }); |
||
| 224 | |||
| 225 | access.logout(headers, res, function() {
|
||
| 226 | res.send(JSON.stringify(ret)); |
||
| 227 | if (fullLog) {
|
||
| 228 | console.log(`${timeStamp()} Successfully logged out: `.green +
|
||
|
1 ignored issue
–
show
|
|||
| 229 | req.query.id.yellow + |
||
| 230 | ` (processed in ${new Date() - start} ms)`.green);
|
||
| 231 | } |
||
| 232 | |||
| 233 | }); |
||
| 234 | }); |
||
| 235 | }); |
||
| 236 | }); |
||
| 239 | console.log(`${timeStamp()} The API is now running on port ${port}. Full logging is ${fullLog ? 'enabled' : 'disabled'}`.green); |